/*
 * Copyright (c) 2018. QDU 2015 Java1 Team CompilationError
 */

package team.mai.antexpress.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.mai.antexpress.dao.DelivermanDAO;
import team.mai.antexpress.dao.UserDAO;
import team.mai.antexpress.entities.DelivermanEntity;
import team.mai.antexpress.entities.PermissionGroupEntity;
import team.mai.antexpress.entities.UserEntity;
import team.mai.antexpress.utils.Validator;

import java.util.List;

@Service
public class DelivermanService {

	@Autowired
	private DelivermanDAO delivermanDAO;
	@Autowired
	private UserDAO userDAO;

	public static final int VALIDATION_ERROR = -2;
	public static final int INTERNAL_ERROR = -1;
	public static final int INSUFFICIENT_PRIVILEGE = -3;
	public static final int SUCCESS = 0;

	public int insertDeliverman(String operatorUsername, DelivermanEntity delToInsert) {
		boolean validate = Validator.validateUserCredentials(delToInsert.getName(), delToInsert.getPassword());
		if (!validate)
			return VALIDATION_ERROR;

		PermissionGroupEntity permission = userDAO.getPermissionByUser(operatorUsername);
		if (permission == null)
			return INTERNAL_ERROR;
		if (permission.isAdmin() || permission.isModifyDeliverman()) {
			if (delivermanDAO.insert(delToInsert))
				return SUCCESS;
			else
				return INTERNAL_ERROR;
		} else
			return INSUFFICIENT_PRIVILEGE;
	}
	public DelivermanEntity getDeliverman(String opName, Integer id){
		if(id == null)
			return null;
		else {
			PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(opName);
			if (permissionByUser.isAdmin() || permissionByUser.isLookupDeliverman()) {
				return delivermanDAO.getDelivermanById(id);
			} else {
				return null;
			}
		}
	}
	public DelivermanEntity getDeliverman(String opName, String delivermanname){
		if(delivermanname == null)
			return null;
		else {
			PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(opName);
			if (permissionByUser.isAdmin() || permissionByUser.isLookupDeliverman()) {
				return delivermanDAO.getDelivermanByDelivermanname(delivermanname);
			} else {
				return null;
			}
		}
	}
	public int getDeliverman(String opName, int id){
		PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(opName);
		if (permissionByUser.isAdmin() || permissionByUser.isLookupDeliverman()) {
			if(delivermanDAO.deleteDelivermanById(id))
				return SUCCESS;
			return INTERNAL_ERROR;
		} else {
			return INSUFFICIENT_PRIVILEGE;
		}
	}
	public int updateDeliverman(String opName, DelivermanEntity del){
		PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(opName);
		if (permissionByUser.isAdmin() || permissionByUser.isModifyDeliverman()) {
			if(delivermanDAO.updateDeliverman(del))
				return SUCCESS;
			return INTERNAL_ERROR;
		} else {
			return INSUFFICIENT_PRIVILEGE;
		}
	}
	public List<DelivermanEntity> getAllDelivermen(String opName){
		PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(opName);
		if (permissionByUser.isAdmin() || permissionByUser.isLookupDeliverman()) {
			return delivermanDAO.getAllDelivermans();
		} else {
			return null;
		}
	}

}
